package cn.iocoder.yudao.module.trade.service.order;

import cn.iocoder.yudao.module.trade.convert.order.TradeOrderLogConvert;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderLogDO;
import cn.iocoder.yudao.module.trade.dal.mysql.order.TradeOrderLogMapper;
import cn.iocoder.yudao.module.trade.service.order.bo.TradeOrderLogCreateReqBO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 交易下单日志 Service 实现类
 *
 * @author 陈賝
 * @since 2023/7/6 15:44
 */
@Service
public class TradeOrderLogServiceImpl implements TradeOrderLogService {

    @Resource
    private TradeOrderLogMapper tradeOrderLogMapper;

    @Override
    public void createOrderLog(TradeOrderLogCreateReqBO createReqBO) {
        tradeOrderLogMapper.insert(convert(createReqBO));
    }

    private TradeOrderLogDO convert(TradeOrderLogCreateReqBO bean) {
        if ( bean == null ) {
            return null;
        }

        TradeOrderLogDO.TradeOrderLogDOBuilder tradeOrderLogDO = TradeOrderLogDO.builder();

        tradeOrderLogDO.userId( bean.getUserId() );
        tradeOrderLogDO.userType( bean.getUserType() );
        tradeOrderLogDO.orderId( bean.getOrderId() );
        tradeOrderLogDO.beforeStatus( bean.getBeforeStatus() );
        tradeOrderLogDO.afterStatus( bean.getAfterStatus() );
        tradeOrderLogDO.operateType( bean.getOperateType() );
        tradeOrderLogDO.content( bean.getContent() );
        TradeOrderLogDO data = tradeOrderLogDO.build();

        if (bean.getCreateTime() != null){
            data.setCreateTime(bean.getCreateTime());
        }
        if (bean.getUpdateTime() != null){
            data.setUpdateTime(bean.getUpdateTime());
        }
        return data;
    }

    @Override
    public List<TradeOrderLogDO> getOrderLogListByOrderId(Long orderId) {
        return tradeOrderLogMapper.selectListByOrderId(orderId);
    }

}
